/*
 * Copyright (C) 2013   Bob Rutledge
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 * and open the template in the editor.
 */
package org.lreqpcr.ui_components;

import java.text.DecimalFormat;
import org.lreqpcr.core.data_objects.Profile;
import org.lreqpcr.core.data_processing.ProfileSummary;
import org.lreqpcr.core.utilities.FormatingUtilities;

/**
 *
 * @author Bob Rutledge
 */
public class NonlinearRegressionParam extends javax.swing.JPanel {

    private Profile profile;
    private DecimalFormat df = new DecimalFormat();
    private DecimalFormat dfE = new DecimalFormat("0.00E0");

    /**
     * Creates new form NonlinearRegressionParam
     */
    public NonlinearRegressionParam() {
        initComponents();
    }

    public void updateDisplay(ProfileSummary prfSum) {
        profile = prfSum.getProfile();
//        if(profile.isExcluded()){
//            clearPanel();
//            failureLabel.setText("Profile Excluded");
//            return;
//        }
        if(!profile.didNonlinearRegressionSucceed()){
            clearPanel();
            failureLabel.setVisible(true);
            return;
        }
        failureLabel.setVisible(false);
        foLabel.setVisible(true);
        emaxLabel.setVisible(true);
        fmaxLabel.setVisible(true);
        fbLabel.setVisible(true);
        fbslopeLabel.setVisible(true);
        String CV;
        df.applyPattern("#0.0%");
        CV = df.format(profile.getNrFoSD() / profile.getNrFo() * 100);
        nrFoDisplay.setText(dfE.format(profile.getNrFo()) + " ±" + CV);
        CV = df.format(profile.getNrEmaxSD() / profile.getNrEmax() * 100);
        nrEmaxDisplay.setText(df.format(profile.getNrEmax()) + " ±" + CV);
        df.applyPattern("#0.0%");
        CV = df.format(profile.getNrFmaxSD() / profile.getNrFmax() * 100);
        df.applyPattern(FormatingUtilities.decimalFormatPattern(profile.getNrFmax())); 
        nrFmaxDisplay.setText(df.format(profile.getNrFmax()) + " ±" + CV);
        df.applyPattern("#0.0%");
        CV = df.format(profile.getNrFbSD() / profile.getNrFb() * 100);
        double fb = profile.getNrFb();
        df.applyPattern(FormatingUtilities.decimalFormatPattern(fb));
        nrFbDisplay.setText((df.format(profile.getNrFb()) + " ±" + CV));
        df.applyPattern("#0.0%");
        CV = df.format(profile.getNrFbSlopeSD() / Math.abs(profile.getNrFbSlope())* 100);
        df.applyPattern(FormatingUtilities.decimalFormatPattern(profile.getNrFbSlope()));
        nrFbSlopeDisplay.setText(df.format(profile.getNrFbSlope()) + " ±" + CV);
    }

    void clearPanel() {
        foLabel.setVisible(false);
        nrFoDisplay.setText("");
        emaxLabel.setVisible(false);
        nrEmaxDisplay.setText("");
        fmaxLabel.setVisible(false);
        nrFmaxDisplay.setText("");
        fbLabel.setVisible(false);
        nrFbDisplay.setText("");
        fbslopeLabel.setVisible(false);
        nrFbSlopeDisplay.setText("");
        failureLabel.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        foLabel = new javax.swing.JLabel();
        nrFoDisplay = new javax.swing.JLabel();
        emaxLabel = new javax.swing.JLabel();
        nrEmaxDisplay = new javax.swing.JLabel();
        fmaxLabel = new javax.swing.JLabel();
        nrFmaxDisplay = new javax.swing.JLabel();
        fbLabel = new javax.swing.JLabel();
        nrFbDisplay = new javax.swing.JLabel();
        fbslopeLabel = new javax.swing.JLabel();
        nrFbSlopeDisplay = new javax.swing.JLabel();
        failureLabel = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(204, 0, 51))); // NOI18N
        setMaximumSize(new java.awt.Dimension(240, 117));
        setMinimumSize(new java.awt.Dimension(240, 117));
        setPreferredSize(new java.awt.Dimension(240, 117));

        org.openide.awt.Mnemonics.setLocalizedText(foLabel, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.foLabel.text")); // NOI18N

        nrFoDisplay.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(nrFoDisplay, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.nrFoDisplay.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(emaxLabel, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.emaxLabel.text")); // NOI18N

        nrEmaxDisplay.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(nrEmaxDisplay, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.nrEmaxDisplay.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(fmaxLabel, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.fmaxLabel.text")); // NOI18N

        nrFmaxDisplay.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(nrFmaxDisplay, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.nrFmaxDisplay.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(fbLabel, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.fbLabel.text")); // NOI18N

        nrFbDisplay.setForeground(new java.awt.Color(255, 51, 51));
        org.openide.awt.Mnemonics.setLocalizedText(nrFbDisplay, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.nrFbDisplay.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(fbslopeLabel, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.fbslopeLabel.text")); // NOI18N

        nrFbSlopeDisplay.setForeground(new java.awt.Color(255, 51, 51));
        org.openide.awt.Mnemonics.setLocalizedText(nrFbSlopeDisplay, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.nrFbSlopeDisplay.text")); // NOI18N

        failureLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        failureLabel.setForeground(new java.awt.Color(204, 0, 51));
        org.openide.awt.Mnemonics.setLocalizedText(failureLabel, org.openide.util.NbBundle.getMessage(NonlinearRegressionParam.class, "NonlinearRegressionParam.failureLabel.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(fmaxLabel)
                            .addComponent(emaxLabel, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nrFmaxDisplay)
                            .addComponent(nrEmaxDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(fbLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nrFbDisplay))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(fbslopeLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nrFbSlopeDisplay)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(foLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nrFoDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(failureLabel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emaxLabel)
                    .addComponent(nrEmaxDisplay))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(foLabel)
                    .addComponent(nrFoDisplay)
                    .addComponent(failureLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fmaxLabel)
                    .addComponent(nrFmaxDisplay))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fbLabel)
                    .addComponent(nrFbDisplay))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fbslopeLabel)
                    .addComponent(nrFbSlopeDisplay)))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel emaxLabel;
    private javax.swing.JLabel failureLabel;
    private javax.swing.JLabel fbLabel;
    private javax.swing.JLabel fbslopeLabel;
    private javax.swing.JLabel fmaxLabel;
    private javax.swing.JLabel foLabel;
    private javax.swing.JLabel nrEmaxDisplay;
    private javax.swing.JLabel nrFbDisplay;
    private javax.swing.JLabel nrFbSlopeDisplay;
    private javax.swing.JLabel nrFmaxDisplay;
    private javax.swing.JLabel nrFoDisplay;
    // End of variables declaration//GEN-END:variables
}
